package com.service;

import com.mapper.SysUserMapper;
import com.pojo.Supplier;
import com.pojo.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 用户登录
     * @param account
     * @param password
     * @return
     */
    @Override
    public SysUser login(String account, String password) {
        if(account==null||password==null){
            return null;
        }
        SysUser sysUser=sysUserMapper.getByAccount(account);
        if(sysUser==null){
            return null;

        }
        if(!sysUser.getPassword().equals(password)){
            return null;
        }
        return sysUser;
    }

    @Override
    public boolean add(SysUser sysUser) {
        boolean result=false;
        if(sysUserMapper.add(sysUser)==1){
            result=true;
        }
        return result;
    }

    @Override
    public List<SysUser> getUserList(String queryRealName, String queryRoleName, Integer pageBegin, Integer pageSize) {
        List<SysUser> userList = new ArrayList<>();
        try{
            userList = sysUserMapper.getUserList(queryRealName,queryRoleName,((pageBegin-1)*pageSize),pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int count(String queryRealName,  String queryRoleName){
        int count = 0;
        try{
            count = sysUserMapper.count(queryRealName, queryRoleName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int getUpdatePwd(int id, String newPassword) {
        return sysUserMapper.getUpdatePwd(id, newPassword);
    }

    @Override
    public int checkOldPwd(int id, String oldPassword) {
        return sysUserMapper.checkOldPwd(id,oldPassword);
    }

    @Override
    public SysUser getUserById(String id) {
        return sysUserMapper.getUserById(id);
    }
}
